﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.ComponentModel;

namespace dkProfiler
{
    public class Stats
    {

        Profile profile;
        BaseStatSource baseStatSource;

        public BaseStat Health { get; set; }
        public StrengthStat Strength { get; set; }
        public AgilityStat Agility { get; set; }
        public StaminaStat Stamina { get; set; }
        public IntellectStat Intellect { get; set; }
        public SpiritStat Spirit { get; set; }

        public AttackPowerStat AttackPower { get; set; }
        public RatingStat Crit { get; set; }
        public RatingStat Hit { get; set; }

        public RatingStat SpellCrit { get; set; }
        public RatingStat SpellHit { get; set; }

        public Stats(Profile profile)
        {
            this.profile = profile;
            baseStatSource = new BaseStatSource(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\data\\baseStats.xml");

            Health = new BaseStat();
            Strength = new StrengthStat();
            Agility = new AgilityStat();
            Stamina = new StaminaStat();
            Intellect = new IntellectStat();
            Spirit = new SpiritStat();

            AttackPower = new AttackPowerStat();
            Crit = new RatingStat();
            Hit = new RatingStat();

            SpellCrit = new RatingStat();
            SpellHit = new RatingStat();
        }

        public void Recalculate()
        {
            Strength.Base = baseStatSource.GetBaseStat(profile.Level, profile.Race, "Strength") + (int)profile.Equipment.SumStat("Strength");
            Strength.Effective = Strength.Base;
            Strength.AttackPower = (Strength.Effective - 10) * 2;
            Strength.ParryRating = (int)Math.Round((double)Strength.Effective * 0.25);

            Agility.Base = baseStatSource.GetBaseStat(profile.Level, profile.Race, "Agility") + (int)profile.Equipment.SumStat("Agility");
            Agility.Effective = Agility.Base;
            Agility.CritPercent = Math.Round(StatConverter.CritFromAgility(Agility.Effective, profile.Level), 2);
            Agility.Armor = Agility.Effective * 2;
            Agility.DodgePercent = Math.Round(StatConverter.DodgeFromAgility(Agility.Effective, profile.Level), 2);

            Stamina.Base = baseStatSource.GetBaseStat(profile.Level, profile.Race, "Stamina") + (int)profile.Equipment.SumStat("Stamina");
            Stamina.Effective = Stamina.Base;
            Stamina.Health = (Stamina.Effective - 18) * 10;

            Intellect.Base = baseStatSource.GetBaseStat(profile.Level, profile.Race, "Intellect") + (int)profile.Equipment.SumStat("Intellect");
            Intellect.Effective = Intellect.Base;
            Intellect.SpellCritPercent = Math.Round(StatConverter.SpellCritFromIntellect(Intellect.Effective, profile.Level), 2);

            Spirit.Base = baseStatSource.GetBaseStat(profile.Level, profile.Race, "Spirit") + (int)profile.Equipment.SumStat("Spirit");
            Spirit.Effective = Spirit.Base;
            Spirit.HealthRegen = (int)Math.Floor((double)Spirit.Effective * 0.375d);

            Health.Base = baseStatSource.GetBaseStat(profile.Level, profile.Race, "Health") + Stamina.Health;
            Health.Effective = Health.Base + (int)profile.Equipment.SumStat("Health");

            AttackPower.Base = Strength.AttackPower;
            AttackPower.Effective = AttackPower.Base + (int)profile.Equipment.SumStat("AttackPower");
            AttackPower.DPS = Math.Round((double)AttackPower.Effective / 14d, 2);

            Crit.Rating = 0;
            Crit.IncreasePercent = Math.Round(StatConverter.CritFromRating(Crit.Rating, profile.Level), 2);
            Crit.Percent = Math.Round(Agility.CritPercent + Crit.IncreasePercent, 2);

            Hit.Rating = 0;
            Hit.IncreasePercent = Math.Round(StatConverter.HitFromRating(Hit.Rating, profile.Level), 2);
            Hit.Percent = Math.Round(Hit.IncreasePercent, 2);

            SpellCrit.Rating = 0;
            SpellCrit.IncreasePercent = Math.Round(StatConverter.SpellCritFromRating(SpellCrit.Rating, profile.Level), 2);
            SpellCrit.Percent = Math.Round(Intellect.SpellCritPercent + SpellCrit.IncreasePercent, 2);

            SpellHit.Rating = 0;
            SpellHit.IncreasePercent = Math.Round(StatConverter.SpellHitFromRating(SpellHit.Rating, profile.Level), 2);
            SpellHit.Percent = Math.Round(SpellHit.IncreasePercent, 2);
        }
    }
}
